<template>
  <div class="index-container">
    <div class="warning-info-wrap">
      <el-row>
        <el-col :span="4">
          <el-button type="primary" @click="handleClick">批量处理</el-button>
        </el-col>
        <el-col :span="20">
          <div class="search" style="text-align: right;">
            <el-row :gutter="20">
              <el-col :xs="8" :sm="8" :md="8" :lg="8" :xl="8">
                <el-date-picker
                  v-model="selectForm.start_time"
                  value-format="yyyy-MM-dd"
                  format="yyyy 年 MM 月 dd 日"
                  placeholder="预约开始"
                  size="medium"
                  style="width: 45%;"
                ></el-date-picker>
                <span style="padding: 0 10px;">~</span>
                <el-date-picker
                  v-model="selectForm.end_time"
                  value-format="yyyy-MM-dd"
                  format="yyyy 年 MM 月 dd 日"
                  placeholder="预约结束"
                  size="medium"
                  style="width: 45%;"
                ></el-date-picker>
                &nbsp;
              </el-col>
              <el-col :xs="2" :sm="2" :md="2" :lg="2" :xl="2">
                <el-button
                  size="medium"
                  type="primary"
                  icon="el-icon-search"
                  class="fr"
                  @click="searchHandleClick"
                >
                  搜索
                </el-button>
              </el-col>
            </el-row>
          </div>
        </el-col>
      </el-row>
    </div>
    <div class="main-content">
      <app-table
        :page-data="pageData"
        :table-columns="tableColumns"
        :table-row-class-name="tableRowClassName"
        @handleCheckedRows="handleCheckedRows"
        @current-change="handleCurrentChange"
      >
        <template slot-scope="scope">
          <el-button
            v-if="scope.row.status == 1"
            size="mini"
            type="primary"
            @click="handleIotUpdate(scope.row)"
          >
            处理
          </el-button>
          <el-button
            v-else
            size="mini"
            type="primary"
            disabled
          >
            已处理
          </el-button>
        </template>
      </app-table>
    </div>
  </div>
</template>
<script>
  import appTable from "@/components/AppTable/index";
  import { deviceIotlog } from "@/api/device";
  import { tenBitTimestamp } from '@/utils'
  import { iotLogupdate } from "@/api/bedList";
  import mixins from '@/utils/mixins.js';
  export default {
    name: "Role",
    components: { appTable },
    mixins: [mixins],
    data() {
      return {
        key: Math.random(),
        currentSelectedData: [],
        pageData: {},
        tableColumns: [
          { prop: "code", label: "设备编号" },
          { prop: "msg", label: "报警名称" },
          { prop: "location", label: "备注内容",  width: 400 },
          { prop: "create_time", label: "响应时间", formatter: this.timeFormatter },
        ],
        selectForm: {
          page_size: 20
        }
      };
    },
    computed: {

    },
    created() {},
    beforeDestroy() {},
    mounted() {
      this.getTableData();
    },
    methods: {
      handleClick() {
        const _this = this;
        if (this.currentSelectedData.length === 0) {
          this.$message({
            message: "请至少选择一条您要处理的内容",
            type: "warning",
          });
          return false
        }
        let idArr = []
        this.currentSelectedData.forEach(item => {
          idArr.push(item.id)
        })
        let params = {
          id: idArr.join(",")
        };
        this.$confirm("确定将所选数据全部处理, 是否继续?", "提示", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning",
        })
        .then(() => {
          iotLogupdate("get", params).then(res => {
            _this.getTableData();
            _this.closeAllNotification();
          })
        })
        .catch(() => {

        });
      },
      searchHandleClick() {
        this.getTableData();
      },
      handleCheckedRows(rows) {
        if (rows && rows instanceof Array && rows.length > 0) {
          this.currentSelectedData = rows;
        } else {
          this.currentSelectedData = [];
        }
      },
      handleIotUpdate(row) {
        iotLogupdate("get", {id: row.id}).then(res => {
          this.getTableData();
          this.closeAllNotification();
        })
      },
      tableRowClassName({row}) {
        if (row.status == 1) {
          return 'warning-row-cell';
        }
        return '';
      },
      timeFormatter(row) {
        if(row.create_time){
          return tenBitTimestamp(row.create_time)
        }
        return "";
      },
      statusFormatter(row) {
        if(row.status == 1) {
          return "未处理"
        } else if(row.status == 2) {
          return "已处理"
        }
        return ""
      },
      typeFormatter(row) {
        let type = ""
        switch (row.type) {
          case 1:
            type = "灯"
            break;
          case 2:
            type = "SOS"
            break;
          case 3:
            type = "门禁"
            break;
          case 4:
            type = "可燃气体"
            break;
          case 5:
            type = "人体红外传感"
            break;
          case 6:
            type = "烟雾"
            break;
          case 7:
            type = "窗帘"
            break;
          case 8:
            type = "漏水检测"
            break;
          case 9:
            type = "温湿度检测"
            break;
          case 10:
            type = "安防报警"
            break;
          case 11:
            type = "床垫报警"
            break;
          case 12:
            type = "尿湿报警"
            break;
        }
        // if(row.location == 'false') {

        // } else {
        //   type = JSON.parse(row.location)[]
        // }
        return type
      },
      toGB2312(str) {
        return unescape(str.replace(/u/gi, '\\u').replace(/\\u/gi, '%u'));
      },
      contFormatter(row) {
        let type = ""
        let location = JSON.parse(row.location);
        let content = JSON.parse(row.content);
        let name = "";
        let floor_code = "";
        let roomBedCont = "";
        if(location.building_name) {
          name = location.building_name && this.toGB2312(location.building_name)
        }
        if(location.name) {
          name = location.name && this.toGB2312(location.name)
        }

        if(location.floor_code) {
          floor_code = location.floor_code;
        }

        if(location.floor_num) {
           floor_code = location.floor_num + "楼"
        }

        switch (row.type) {
          case 1:
            type = "灯"
            break;
          case 2:
            type = !location || location.length == 0 ? "报警器响了" : name + '-' + floor_code + '-' + location.room_code + "报警器响了"
            break;
          case 3:
            if(!location || location.length == 0) {
              if(content.body['datas'][0]['zone_status'] == '0021') {
                type = "房间门打开了"
              } else if(content.body['datas'][0]['zone_status'] == '0020') {
                type = "房间门关闭了"
              }
            } else {
              if(content.body['datas'][0]['zone_status'] == '0021') {
                type = name + '-' + floor_code + '-' + location.room_code + "房间门打开了"
              } else if(content.body['datas'][0]['zone_status'] == '0020'){
                type = name + '-' + floor_code + '-' + location.room_code + "房间门关闭了"
              }
            }
            break;
          case 4:
            type = !location || location.length == 0 ? "可燃气体报警" : name + '-' + floor_code + '-' + location.room_code + "可燃气体报警"
            break;
          case 5:
            if(!location || location.length == 0) {
              if(content.body['datas'][0]['zone_status'] == '0031') {
                type = "请注意查看，检测有人"
              } else if(content.body['datas'][0]['zone_status'] == '0000') {
                type = "请注意查看，检测无人"
              }
            } else {
              if(content.body['datas'][0]['zone_status'] == '0031') {
                type = name + '-' + floor_code + '-' + location.room_code + "请注意查看，检测有人"
              } else if(content.body['datas'][0]['zone_status'] == '0000'){
                type = name + '-' + floor_code + '-' + location.room_code + "请注意查看，检测无人"
              }
            }
            break;
          case 6:
            type = !location || location.length == 0 ? "烟雾报警" : name + '-' + floor_code + '-' + location.room_code + "烟雾报警"
            break;
          case 7:
            type = !location || location.length == 0 ? "窗帘打开了" : name + '-' + floor_code + '-' + location.room_code + "窗帘打开了"
            break;
          case 8:
            type = !location || location.length == 0 ? "漏水报警" : name + '-' + floor_code + '-' + location.room_code + "漏水报警"
            break;
          case 9:
            type = "温湿度检测"
            break;
          case 10:
            type = !location || location.length == 0 ? "安防报警" : name + '-' + floor_code + '-' + location.room_code + "安防报警"
            break;
          case 11:
            if(content.value == 0) {
              roomBedCont = roomBedCont += "床位有人"
            } else {
              roomBedCont = roomBedCont += "床位无人"
            }
            // if(content?.data.length) {
            //   if(content.data[1].id == 1) {
            //     if(content.data[1].value == 0) {
            //       roomBedCont += "床位干燥"
            //     } else {
            //       roomBedCont += "床位尿湿"
            //     }
            //   }
            // }
            type = !location || location.length == 0 ? "床垫报警" : name + '-' + floor_code + '-' + location.room_code + '_' + location.place + "床" + '-' + roomBedCont
            break;
            case 12:
            if(content.value == 1) {
              roomBedCont += "床位尿湿"
            }
            type = !location || location.length == 0 ? "尿湿报警" : name + '-' + floor_code + '-' + location.room_code + '_' + location.place + "床" + '-' + roomBedCont
            break;
        }
        return type
      },
      getTableData() {
        let data = this.selectForm;
        deviceIotlog("get", data).then((res) => {
          this.pageData = Object.assign(res.data, {page_size: 20, type: -1});
          // res.data.list.map(item => {

          // })
        });
      },
      handleCurrentChange(val) {
        this.selectForm.page = val;
        this.getTableData();
      },
    },
  };
</script>
<style lang="scss" scoped>
.table-layout {
  padding: 20px;
  border-left: 1px solid #DCDFE6;
  border-right: 1px solid #DCDFE6;
  border-bottom: 1px solid #DCDFE6;
}
.warning-info-wrap{
  margin-bottom: 10px;
}
::v-deep{
  td{
    background: #11203f;
  }
  .warning-row-cell{
    td {
      padding: 10px 0 !important;
      font-size: 16px !important;
      .cell {
        font-size: 18px !important;
        color: #fff !important;
      }
    }
  }
}
</style>
